﻿using System;
using System.Windows.Input;
using CleanBackup.Helpers;
using CleanBackupDLL;

namespace CleanBackup.ViewModel
{
	class MainWindowViewModel : ViewModelBase
	{
		Backup bla;

		public MainWindowViewModel()
		{
			bla = new Backup();
			bla.ChangedStatus += new Backup.ChangedStatusEventHandler(bla_ChangedStatus);
			bla.ChangedProgress += new Backup.ChangedProgressEventHandler(bla_ChangedProgress);

			bla.Initialize();
		}

		void bla_ChangedProgress(object sender, EventArgs e)
		{
			Progress test = e as Progress;

			ProcessedFiles = test.successFiles;
			ErrorFiles = test.errorFiles;
		}

		void bla_ChangedStatus(object sender, EventArgs e)
		{
			BackupStatus test = e as BackupStatus;

			NextBackup = test.NextBackupTime;
			TotalFiles = test.TotalFiles;
			Status = test.State;
			
			timerOn = test.IsTimerRunning;
			srcPath = test.SrcPath;
			destPath = test.DestPath;
		}

		void forceSync()
		{
			bla.StartBackup();
		}

		void SaveConfig()
		{
			bla.Save();
		}

		void cancel()
		{
			bla.Cancel();
		}

		void SetTimer()
		{
			bla.SetTimer(TimerOn);
		}

		void SetPath()
		{
			bla.SetPath(SrcPath, DestPath);
		}

		#region Commands
		private RelayCommand forceSyncCmd;
		public ICommand ForceSyncCmd
		{
			get
			{
				if (forceSyncCmd == null)
				{
					forceSyncCmd = new RelayCommand(param => forceSync(), param => CanSyncExecute);
				}
				return forceSyncCmd;
			}
		}

		private RelayCommand saveConfigCmd;
		public ICommand SaveConfigCmd
		{
			get
			{
				if (saveConfigCmd == null)
				{
					saveConfigCmd = new RelayCommand(param => SaveConfig(), param => CanSyncExecute);
				}
				return saveConfigCmd;
			}
		}

		private RelayCommand cancelCmd;
		public ICommand CancelCmd
		{
			get
			{
				if (cancelCmd == null)
				{
					cancelCmd = new RelayCommand(param => cancel());
				}
				return cancelCmd;
			}
		}
		#endregion

		#region Members
		private string destPath;
		public string DestPath
		{
			get
			{
				return destPath;
			}
			set
			{
				destPath = value;
				SetPath();
				OnPropertyChanged("DestPath");
				OnPropertyChanged("CanSyncExecute");
			}
		}

		private string srcPath;
		public string SrcPath
		{
			get
			{
				return srcPath;
			}
			set
			{
				srcPath = value;
				SetPath();
				OnPropertyChanged("SrcPath");
				OnPropertyChanged("CanSyncExecute");
			}
		}

		private States status;
		public States Status
		{
			get
			{
				return status;
			}
			set
			{
				status = value;
				OnPropertyChanged("Status");
				OnPropertyChanged("CanChangePath");
			}
		}


		private DateTime nextBackup;
		public DateTime NextBackup
		{
			get
			{
				return nextBackup;
			}
			set
			{
				nextBackup = value;
				OnPropertyChanged("NextBackup");
			}
		}


		private int errorFiles;
		public int ErrorFiles
		{
			get
			{
				return errorFiles;
			}
			set
			{
				errorFiles = value;
				OnPropertyChanged("ErrorFiles");
				OnPropertyChanged("Percent");
			}
		}

		private int processedFiles;
		public int ProcessedFiles
		{
			get
			{
				return processedFiles;
			}
			set
			{
				processedFiles = value;
				OnPropertyChanged("ProcessedFiles");
				OnPropertyChanged("Percent");
			}
		}

		private int totalFiles;
		public int TotalFiles
		{
			get
			{
				return totalFiles;
			}
			set
			{
				totalFiles = value;
				OnPropertyChanged("TotalFiles");
				OnPropertyChanged("Percent");
			}
		}

		private bool timerOn;
		public bool TimerOn
		{
			get
			{
				return timerOn;
			}
			set
			{
				timerOn = value;
				SetTimer();
				OnPropertyChanged("TimerOn");
				OnPropertyChanged("CanChangePath");
			}
		}

		public double Percent
		{
			get
			{
				if (TotalFiles == 0)
					return 0;

				return ((double)(ProcessedFiles + ErrorFiles) / (double)TotalFiles) * 100d;
			}
		}

		public bool IsBackupTimeVisible
		{
			get
			{
				return NextBackup == DateTime.MinValue ? false : true;
			}
		}

		public bool CanChangePath
		{
			get
			{
				return Status == States.RUNNING ? false : TimerOn == true ? false : true;
			}
		}

		public bool CanSyncExecute
		{
			get
			{
				return String.IsNullOrEmpty(SrcPath) || String.IsNullOrEmpty(DestPath) ? false : true;
			}
		}
		#endregion
	}
}
